package q118;

import java.util.ArrayList;
import java.util.List;

/**
 * 118. 杨辉三角
 */
public class Solution {

    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        res.add(list);

        for (int level = 2; level <= numRows; level++) {
            List<Integer> preLevelList = res.get(level - 2);
            List<Integer> curLevelList = new ArrayList<>(level);
            for (int i = 0; i < level; i++) {
                if (i == 0 || i == level - 1) {
                    curLevelList.add(1);
                } else {
                    curLevelList.add(preLevelList.get(i - 1) + preLevelList.get(i));
                }
            }
            res.add(curLevelList);
        }
        return res;
    }
}
